package com.gitee.elf.core.entity

import com.gitee.elf.api.common.MetaEntityWatcher
import com.gitee.elf.api.common.MetaWatcher
import com.gitee.elf.api.event.ElfEntitySpawnEvent
import com.gitee.elf.api.nms.EntityTypeManager
import com.gitee.elf.core.data.DataBoolean
import com.gitee.elf.core.data.DataDouble
import com.gitee.elf.core.data.DataFloat
import com.gitee.elf.core.data.DataString
import com.gitee.elf.core.entity.MetadataContainer.Companion.safeData
import org.bukkit.entity.Entity
import org.bukkit.entity.EntityType
import org.bukkit.entity.LivingEntity
import taboolib.common.platform.function.info
import taboolib.module.chat.colored
import taboolib.platform.util.setMeta

abstract class AbstractBukkitProxyEntity<T : Entity>(instance: EntityInstance) : AbstractProxyEntity<T>(instance) {

    abstract val bukkitType: EntityType

    var entity: T? = null

    val customName = MetaEntityWatcher<DataString>("custom-name", this) {
        entity?.customName = value
    }

    val nameVisiable = MetaEntityWatcher<DataBoolean>("name-visiable",this) {
        entity?.isCustomNameVisible = value
    }

    val moveSpeed = MetaWatcher<DataFloat>("move-speed",this)

    val jumpHeight = MetaWatcher<DataDouble>("jump-height",this)

    val ascendSpeed = MetaWatcher<DataDouble>("ascend-speed",this)

    @Suppress("UNCHECKED_CAST")
    override fun onSpawn() {
        entity = EntityTypeManager.INSTANCE.create(instance.ownerLocation,this, bukkitType.name) as T
        entity!!.setMeta("@EntityInstance", instance)
        ElfEntitySpawnEvent(instance, this).call()
        super.onSpawn()
        info("释放宠物")
    }

    override fun onTick() {
    }

    override fun onDestroy() {
        entity?.remove()
        info("收回宠物")
    }

    fun ifLivingEntity(block: LivingEntity.() -> Unit) {
        if (entity is LivingEntity) {
            block(entity as LivingEntity)
        }
    }

    fun addPassenger(passenger: Entity) {
        entity?.addPassenger(passenger)
    }

}